package com.lagou.service.impl;

import com.lagou.entity.AuthToken;
import com.lagou.feign.ServiceCodeClient;
import com.lagou.repository.AuthTokenRepository;
import com.lagou.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private ServiceCodeClient serviceCodeClient;

    @Autowired
    private AuthTokenRepository authTokenRepository;

    @Override
    public String register(String email, String password, String code, HttpServletResponse response) {
        //判断有没有注册过，注册过直接返回成功
        Boolean registered = isRegistered(email);
        if (registered) {
            return "0";
        }

        //判断验证码是否有效，调用验证码微服务
        String validate = serviceCodeClient.validate(email, code);
        if (!"0".equals(validate)) {
            return validate;
        }

        //生成token，存储到数据库
        AuthToken authToken = new AuthToken();
        authToken.setEmail(email);
        UUID uuid = UUID.randomUUID();
        authToken.setToken(uuid.toString());
        authTokenRepository.save(authToken);

        //将token设置到cookie中
        Cookie cookie = new Cookie("token", authToken.getToken());
        cookie.setPath("/");
        cookie.setMaxAge(60 * 60);
        response.addCookie(cookie);
        return "0";
    }

    @Override
    public Boolean isRegistered(String email) {
        AuthToken authToken = new AuthToken();
        authToken.setEmail(email);
        Example<AuthToken> example = Example.of(authToken);
        List<AuthToken> authTokenList = authTokenRepository.findAll(example);
        if (!CollectionUtils.isEmpty(authTokenList)) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean login(String email, String password, HttpServletResponse response) {
        //判断有没有注册过，没注册过返回失败
        Boolean registered = isRegistered(email);
        if (!registered) {
            return false;
        }

        if (!"123".equals(password)) {
            log.error("密码错误");
            return false;
        }

        //生成token，存储到数据库
        AuthToken authToken = new AuthToken();
        authToken.setEmail(email);
        UUID uuid = UUID.randomUUID();
        authToken.setToken(uuid.toString());

        //将token设置到cookie中
        Cookie cookie = new Cookie("token", authToken.getToken());
        cookie.setPath("/");
        cookie.setMaxAge(60 * 60);
        response.addCookie(cookie);
        return true;
    }

    @Override
    public String info(String token) {
        AuthToken authToken = new AuthToken();
        authToken.setToken(token);
        Example<AuthToken> example = Example.of(authToken);
        List<AuthToken> authTokenList = authTokenRepository.findAll(example);
        if (!CollectionUtils.isEmpty(authTokenList)) {
            return authTokenList.get(0).getEmail();
        }
        return null;
    }
}
